/*
 * This file is part of the OdinMS Maple Story Server
 * Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
 *                    Matthias Butz <matze@odinms.de>
 *                    Jan Christian Meyer <vimes@odinms.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation. You may not use, modify
 * or distribute this program under any other version of the
 * GNU Affero General Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* Author: Xterminator
	NPC Name: 		The Ticket Gate
	Map(s): 		Victoria Road : Subway Ticketing Booth (103000100)
	Description: 		Ticket Gate
*/
var tickets = Array(4031036, 4031037, 4031038, 4031711);
var maps = Array(103000900, 103000903, 103000906, 600010001);
var mapnames = Array("Construction Site B1", "Construction Site B2", "Construction Site B3","New Leaf City (Normal)");
var sw;

function start() {
	status = -1;
	action(1, 0, 0);
	sw = cm.getEventManager("Subway");
}

function action(mode, type, selection) {
	if (mode == -1) {
		cm.dispose();
	} else {
		if (status == 0 && mode == 0) {
			cm.dispose();
			return;
		} else if (status == 1 && mode == 0) {
			cm.sendNext("You must have some business to take care of here, right?");
			cm.dispose();
			return;
		}
		if (mode == 1)
			status++;
		else
			status--;
		if (status == 0) {
			if (!cm.haveItem(4031711) && !cm.haveItem(4031036) && !cm.haveItem(4031037) && !cm.haveItem(4031038)) {
				cm.sendNext("Here's the ticket reader. You are not allowed in without the ticket.");
				cm.dispose();
			} else {
				menu = "Here's the ticket reader. You will be brought in immediately. Which ticket would you like to use?";
				for (i = 0; i < tickets.length; i++) {
					if (cm.haveItem(tickets[i])) {
						menu += "\r\n#b#L" + i +"##b" + mapnames[i] + "#k#l\r\n";
					}
				}
				cm.sendSimple(menu);
			}
		} else if (status == 1) {
			if (selection < 3) {
				cm.gainItem(tickets[selection], -1);
				cm.warp(maps[selection]);
				cm.dispose();
			} else {
				if (sw != null && sw.getProperty("entry").equals("true")) {
					cm.sendYesNo("It looks like there's plenty of room for this ride. Please have your ticket ready so I can let you in. The ride will be long, but you'll get to your destination just fine. What do you think? Do you want to get on this ride?");
				} else if (sw == null) {
					cm.sendYesNo("Would you like to be sent to New Leaf City?");
				} else {
					cm.sendOk("Sorry, but the subway hasn't arrived yet. Please hold on a little bit longer.");
					cm.dispose();
				}
			}
		} else if (status == 2) {
			cm.gainItem(4031711, -1);
			if (sw != null) {
				cm.warp(600010004);
			} else {
				cm.warp(600010001, 0);
			}
			cm.dispose();
		}
	}
}